package org.soc.timesheet.domain;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TypedQuery;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Configurable
@Entity
@Table(name = "USERS")
@RooJavaBean
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { " 	", "timesheets", "usersLoggeds", "usersProjects" })
@RooJson
@RooJpaActiveRecord(versionField = "", table = "USERS", finders = { "findUsersesByLoginEquals", "findUsersesByIdRole", "findUsersesByLogin" })
@JsonIgnoreProperties({"timesheets", "usersLoggeds", "usersProjects"  })
public class Users {

	@PersistenceContext
    transient EntityManager entityManager;

	public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

	public static final EntityManager entityManager() {
        EntityManager em = new Users().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countUserses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Users o", Long.class).getSingleResult();
    }

	public static List<Users> findAllUserses() {
        return entityManager().createQuery("SELECT o FROM Users o", Users.class).getResultList();
    }

	public static List<Users> findAllUserses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Users o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Users.class).getResultList();
    }

	public static Users findUsers(Integer id) {
        if (id == null) return null;
        return entityManager().find(Users.class, id);
    }

	public static List<Users> findUsersEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Users o", Users.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public static List<Users> findUsersEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Users o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Users.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Users attached = Users.findUsers(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Users merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Users merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toJson() {
        return new JSONSerializer()
        .exclude("*.class").serialize(this);
    }

	public String toJson(String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(this);
    }

	public static Users fromJsonToUsers(String json) {
        return new JSONDeserializer<Users>()
        .use(null, Users.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Users> collection) {
        return new JSONSerializer()
        .exclude("*.class").serialize(collection);
    }

	public static String toJsonArray(Collection<Users> collection, String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(collection);
    }

	public static Collection<Users> fromJsonArrayToUserses(String json) {
        return new JSONDeserializer<List<Users>>()
        .use("values", Users.class).deserialize(json);
    }

	@OneToMany(mappedBy = "idUsers")
    private Set<Emails> emailss;

	@OneToMany(mappedBy = "idUser")
    private Set<Timesheet> timesheets;

	@OneToMany(mappedBy = "idUser")
    private Set<UsersLogged> usersLoggeds;

	@OneToMany(mappedBy = "idUser")
    private Set<UsersProject> usersProjects;

	@ManyToOne
    @JoinColumn(name = "ID_CUSTOMER", referencedColumnName = "ID", nullable = false)
    private CustomersEnabled idCustomer;

	@ManyToOne
    @JoinColumn(name = "ID_ROLE", referencedColumnName = "ID", nullable = false)
    private Roles idRole;

	@Column(name = "LOGIN", length = 45)
    private String login;

	@Column(name = "PASSWORD")
    private byte[] password;

	@Column(name = "FIRST_LOGIN")
    private Boolean firstLogin;

	@Column(name = "LOCKED")
    private Boolean locked;

	public Set<Emails> getEmailss() {
        return emailss;
    }

	public void setEmailss(Set<Emails> emailss) {
        this.emailss = emailss;
    }

	public Set<Timesheet> getTimesheets() {
        return timesheets;
    }

	public void setTimesheets(Set<Timesheet> timesheets) {
        this.timesheets = timesheets;
    }

	public Set<UsersLogged> getUsersLoggeds() {
        return usersLoggeds;
    }

	public void setUsersLoggeds(Set<UsersLogged> usersLoggeds) {
        this.usersLoggeds = usersLoggeds;
    }

	public Set<UsersProject> getUsersProjects() {
        return usersProjects;
    }

	public void setUsersProjects(Set<UsersProject> usersProjects) {
        this.usersProjects = usersProjects;
    }

	public CustomersEnabled getIdCustomer() {
        return idCustomer;
    }

	public void setIdCustomer(CustomersEnabled idCustomer) {
        this.idCustomer = idCustomer;
    }

	public Roles getIdRole() {
        return idRole;
    }

	public void setIdRole(Roles idRole) {
        this.idRole = idRole;
    }

	public String getLogin() {
        return login;
    }

	public void setLogin(String login) {
        this.login = login;
    }

	public byte[] getPassword() {
        return password;
    }

	public void setPassword(byte[] password) {
        this.password = password;
    }

	public Boolean getFirstLogin() {
        return firstLogin;
    }

	public void setFirstLogin(Boolean firstLogin) {
        this.firstLogin = firstLogin;
    }

	public Boolean getLocked() {
        return locked;
    }

	public void setLocked(Boolean locked) {
        this.locked = locked;
    }

	public static Long countFindUsersesByIdRole(Roles idRole) {
        if (idRole == null) throw new IllegalArgumentException("The idRole argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery q = em.createQuery("SELECT COUNT(o) FROM Users AS o WHERE o.idRole = :idRole", Long.class);
        q.setParameter("idRole", idRole);
        return ((Long) q.getSingleResult());
    }

	public static Long countFindUsersesByLogin(String login) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery q = em.createQuery("SELECT COUNT(o) FROM Users AS o WHERE o.login = :login", Long.class);
        q.setParameter("login", login);
        return ((Long) q.getSingleResult());
    }

	public static Long countFindUsersesByLoginEquals(String login) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery q = em.createQuery("SELECT COUNT(o) FROM Users AS o WHERE o.login = :login", Long.class);
        q.setParameter("login", login);
        return ((Long) q.getSingleResult());
    }

	public static TypedQuery<Users> findUsersesByIdRole(Roles idRole) {
        if (idRole == null) throw new IllegalArgumentException("The idRole argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery<Users> q = em.createQuery("SELECT o FROM Users AS o WHERE o.idRole = :idRole", Users.class);
        q.setParameter("idRole", idRole);
        return q;
    }

	public static TypedQuery<Users> findUsersesByIdRole(Roles idRole, String sortFieldName, String sortOrder) {
        if (idRole == null) throw new IllegalArgumentException("The idRole argument is required");
        EntityManager em = Users.entityManager();
        String jpaQuery = "SELECT o FROM Users AS o WHERE o.idRole = :idRole";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        TypedQuery<Users> q = em.createQuery(jpaQuery, Users.class);
        q.setParameter("idRole", idRole);
        return q;
    }

	public static TypedQuery<Users> findUsersesByLogin(String login) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery<Users> q = em.createQuery("SELECT o FROM Users AS o WHERE o.login = :login", Users.class);
        q.setParameter("login", login);
        return q;
    }

	public static TypedQuery<Users> findUsersesByLogin(String login, String sortFieldName, String sortOrder) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        String jpaQuery = "SELECT o FROM Users AS o WHERE o.login = :login";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        TypedQuery<Users> q = em.createQuery(jpaQuery, Users.class);
        q.setParameter("login", login);
        return q;
    }

	public static TypedQuery<Users> findUsersesByLoginEquals(String login) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        TypedQuery<Users> q = em.createQuery("SELECT o FROM Users AS o WHERE o.login = :login", Users.class);
        q.setParameter("login", login);
        return q;
    }

	public static TypedQuery<Users> findUsersesByLoginEquals(String login, String sortFieldName, String sortOrder) {
        if (login == null || login.length() == 0) throw new IllegalArgumentException("The login argument is required");
        EntityManager em = Users.entityManager();
        String jpaQuery = "SELECT o FROM Users AS o WHERE o.login = :login";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        TypedQuery<Users> q = em.createQuery(jpaQuery, Users.class);
        q.setParameter("login", login);
        return q;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;

	public Integer getId() {
        return this.id;
    }

	public void setId(Integer id) {
        this.id = id;
    }

	public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("emailss", "timesheets", "usersLoggeds", "usersProjects", "idCustomer", "idRole").toString();
    }
}
